package Assignment03;

import java.util.LinkedList;

public class AddressBook {
	public LinkedList<AddressBookItem> ab;
	private int bookSize;

	public AddressBook() {
		ab = new LinkedList<AddressBookItem>();
		bookSize = 0;

	}

	/**
	 * 
	 * @return The current size of the address book
	 */
	public int GetAddressBookSize() {
		return bookSize;
	}

	/**
	 * 
	 * @param item
	 * @return true if add was successful
	 * @return false if add failed due to full address book
	 */
	public boolean AddAddressBookItem(AddressBookItem item) {

		if (bookSize < 500) {
			ab.add(item);
			bookSize++;
			return true;
		} else {
			System.out.println("Address book is full");
			return false;
		}
	}

	/**
	 * 
	 * @param FirstName
	 * @param LastName
	 * @return The first entry in the Address book that matches the first and
	 *         last name
	 */
	private AddressBookItem FindEntryByName(String FirstName, String LastName) {

		for (int i = 0; i < ab.size(); i++) {
			if (ab.get(i).getPerson().getLastName().toUpperCase()
					.equals(LastName.toUpperCase())) {
				if (ab.get(i).getPerson().getFirstName().toUpperCase()
						.equals(FirstName.toUpperCase())) {
					return ab.get(i);
				}
			}
		}
		return null;
	}

	public void SearchByName(String FirstName, String LastName) {
		AddressBookItem temp = FindEntryByName(FirstName, LastName);

		if (temp == (null)) {
			System.out.println("No entry with that name");
		} else {
			System.out.println(temp.getPerson().getFirstName() + " "
					+ temp.getPerson().getLastName()
					+ " exists in the Address Book");
		}
	}

	/**
	 * Prints the address, phone, and date of birth of a given person
	 * 
	 * @param FirstName
	 * @param LastName
	 */
	public void PrintEntryByName(String FirstName, String LastName) {

		AddressBookItem temp = FindEntryByName(FirstName, LastName);

		if (temp == (null)) {
			System.out.println("No entry with that name");
		} else {
			PrintEntry(temp);
		}
	}

	/**
	 * 
	 * @param item
	 *            The item you would like to print
	 */
	private void PrintEntry(AddressBookItem item) {

		System.out.println(item.getPerson().toString() + "\n"
				+ item.getAddress().toString());
	}

	/**
	 * 
	 * @param LastName
	 * @return A linked list of all of the AddressBook Entries with the given
	 *         last name
	 */
	private LinkedList<AddressBookItem> GetEntriesByLastName(String LastName) {

		LinkedList<AddressBookItem> lastNames = new LinkedList<AddressBookItem>();

		for (int i = 0; i < ab.size(); i++) {
			if (ab.get(i).getPerson().getLastName().toUpperCase()
					.equals(LastName.toUpperCase())) {
				lastNames.add(ab.get(i));
			}

		}

		if (lastNames.size() > 0) {
			return lastNames;
		} else
			return null;

	}

	/**
	 * Prints the results of the Private GetEntriesByLastName method
	 * 
	 * @param LastName
	 */
	public void PrintEntriesByLastName(String LastName) {

		LinkedList<AddressBookItem> results = GetEntriesByLastName(LastName);
		if (results == (null)) {
			System.out.println("No entries with that last name");
		} else {
			for (AddressBookItem a : results) {
				System.out.println(a.getPerson().getLastName() + ", "
						+ a.getPerson().getFirstName());
			}
		}
	}

}
